{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7656f2f2-f649-4c0e-890e-642af5f8a6a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.8/site-packages/sympy/core/relational.py:495: SymPyDeprecationWarning: \n",
      "\n",
      "Eq(expr) with rhs default to 0 has been deprecated since SymPy 1.5.\n",
      "Use Eq(expr, 0) instead. See\n",
      "https://github.com/sympy/sympy/issues/16587 for more info.\n",
      "\n",
      "  SymPyDeprecationWarning(\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle y{\\left(x \\right)} = C_{1} e^{- \\frac{x^{2}}{2}}$"
      ],
      "text/plain": [
       "Eq(y(x), C1*exp(-x**2/2))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import * \n",
    "# https://zhuanlan.zhihu.com/p/412035413\n",
    "# sympy提供了sympy.ode.dsolve()函数用来求解常微分方程。\n",
    "\n",
    "\n",
    "x = symbols('x')\n",
    "y=Function('y')\n",
    "\n",
    "# 常微分方程的通解\n",
    "dsolve(Eq(diff(y(x),x)+x*y(x)), y(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ff4efb5d-7ee4-4440-b83d-29e84a3fc4e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle y{\\left(x \\right)} = \\left(C_{1} \\sin{\\left(\\sqrt{2} x \\right)} + C_{2} \\cos{\\left(\\sqrt{2} x \\right)}\\right) e^{- x}$"
      ],
      "text/plain": [
       "Eq(y(x), (C1*sin(sqrt(2)*x) + C2*cos(sqrt(2)*x))*exp(-x))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 常微分方程的特解\n",
    "dsolve(Eq(y(x).diff(x,x)+2*y(x).diff(x)+3*y(x)), func=y(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1d89cf43-7236-4bc3-90b7-95f29ccdff3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle y{\\left(x \\right)} = \\frac{\\sqrt{2} e^{- x} \\sin{\\left(\\sqrt{2} x \\right)}}{2}$"
      ],
      "text/plain": [
       "Eq(y(x), sqrt(2)*exp(-x)*sin(sqrt(2)*x)/2)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dsolve(Eq(y(x).diff(x,x)+2*y(x).diff(x)+3*y(x)), func=y(x), ics={y(0): 0, y(x).diff(x).subs(x, 0): 1}, n=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c5177ed-48a5-4162-a6d8-290aa38d4584",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
